﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TPCN.DAL;

namespace TPCN.Controls
{
    public partial class uc_menu : System.Web.UI.UserControl
    {
        private string _position;

        public string Position
        {
            get { return _position; }
            set { _position = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                html.Text = GenerateHtmlMenuCode("0");
            }
        }

        protected string GenerateHtmlMenuCode(string parentId)
        {
            if (this._position == "1")
            {
                return BuildMainMenu(parentId);
            }
            return BuildFooterMenu(parentId);
        }

        protected string BuildMainMenu(string parentID)
        {
            string code = String.Empty;
            List<MenuObj> list = new List<MenuObj>();
            list = MenuDAL.GetByPosition("1", parentID);
            if (list.Count > 0)
            {
                code += "<ul>";
                for (int i = 0; i < list.Count; i++)
                {
                    code += "<li><a href=\"" + Page.ResolveUrl(list[i].Link) + "\" target=\"" + list[i].Target + "\">";
                    code += list[i].Name;
                    code += "</a>";
                    code += BuildMainMenu(list[i].MenuID);
                }
                code += "</ul>";
            }
            return code;
        }

        protected string BuildFooterMenu(string parentID)
        {
            string code = String.Empty;
            List<MenuObj> list = new List<MenuObj>();
            list = MenuDAL.GetByPosition("2", parentID);
            if (list.Count > 0)
            {
                for (int i = 0; i < list.Count; i++)
                {
                    code += "<div class=\"footer-cat\">";
                    code += "<p>" + list[i].Name + "</p>";
                    List<MenuObj> l = new List<MenuObj>();
                    l = MenuDAL.GetByPosition("2", list[i].MenuID);
                    if (l.Count > 0)
                    {
                        int count = 0;
                        while (count < l.Count)
                        {
                            if (count % 3 == 0)
                            {
                                code += "<div class=\"footer-ul\">";
                                code += "<ul>";
                            }
                            code += "<li><a href=\"" + Parent.ResolveUrl(l[count].Link) + "\" target=\"" + l[count].Target + "\">";
                            code += l[count].Name + "</a></li>";
                            if (count % 3 == 2 || count == l.Count - 1)
                            {
                                code += "</ul></div>";
                            }
                            count++;
                        }
                    }
                    code += "<div class=\"clear\"></div></div>";
                }
            }
            return code;
        }
    }
}